//  var API_URL = 'https://www.bzhtech.com'
var API_URL = 'https://stg.share1949.com'
//  var API_URL = 'https://www.share1949.com'
// var API_URL = 'http://192.168.0.193:8080';

function fetchApi(params) {
  
  wx.showLoading({
    mask: true,
    title: "加载中..."
  })
  return new Promise((resolve, reject) => {
    wx.request({
      // url: API_URL + '/api/',      
      url: API_URL + '/gapi/commonGateway/api/',
      data: params,
      method: "POST",
      header: {
        'content-type': 'json' // 默认值
      },
      success: function(res) {
        // debugger
        wx.hideLoading();
        wx.stopPullDownRefresh();
        // debugger
        if (res.data.code == 200 && res.data.result.code == 0) {
          resolve(res.data.result);
        } else {

          if (res.data.result.code == 409) {
            //重新登陆
            wx.setStorageSync('userToken', "");
            var pages = getCurrentPages() //获取加载的页面

            var currentPage = pages[pages.length - 1] //获取当前页面的对象
            console .log(currentPage.route)
            if (currentPage.route != 'pages/login/login') {
              wx.showToast({
                title: res.data.result.msg,
                icon: 'none',
                duration: 3000,
                success: function () {
                  setTimeout(function () {
                    wx.reLaunch({
                      url: '../login/login'
                    })
                  }, 3000);
                }
              })
            
            } else {
              resolve(res.data.result);
            }

          }else{
            //显示原因
            wx.showToast({
              title: res.data.result.msg,
              icon: 'none',
              duration: 3000
            })
          }

        }
      },
      fail: function(e) {

        wx.hideLoading();
        wx.stopPullDownRefresh();

        wx.showToast({
          title: '当前环境异常，请稍后再试！',
          icon: 'none',
          duration: 3000
        })
      }
    })
  })
}

function uploadeImage(params) {
  return new Promise((resolve, reject) => {
    wx.uploadFile({
      url: API_URL + '/imgproxy/zhys-img-service/img', // 仅为示例，非真实的接口地址
      filePath: params.filePath,
      name: 'file',
      formData: params.dataBody,
      success(res) {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        var obj = JSON.parse(res.data);
        console.log(obj);
        resolve(obj);
      },
      fail: function(e) {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        console.log(e);
      }
    })
  })
}

module.exports = {
  networkForData: function(method, body) {
    var userToken = wx.getStorageSync('userToken')
    return fetchApi({
        "method": method,
        "token": userToken,
        "dataBody": body
      })
      .then(res => res.data)
      
  },
  networkForUploadImage: function (filePath) {
    var token = wx.getStorageSync('userToken')
    return uploadeImage({
      'filePath': filePath,
      dataBody: {
        "file": 'file',
        "fileName": token,
        "imgSource": 'bvatar'
      }
    })
      .then(res => res)
  },
  API_URL: API_URL

}